import torch
import torch.nn as nn
import torch.nn.functional as F
from functools import partial

class BasicBlock(nn.Module):
  expansion = 1
  def __init__(self, in_channel, out_channel, stride=1, downsample=None):
    super(BasicBlock, self).__init__()

    self.conv1 = nn.Conv2d(in_channels=in_channel, out_channels=out_channel, kernel_size=3, stride=stride, bias=False, padding=1)
    self.bn1 = nn.BatchNorm2d(out_channel)
    self.relu = nn.LeakyReLU()
    self.conv2 = nn.Conv2d(in_channels=out_channel, out_channels=out_channel, kernel_size=3, stride=1, bias=False, padding=1)
    self.bn2 = nn.BatchNorm2d(out_channel)

    self.conv3 = nn.Conv2d(in_channels=out_channel*4, out_channels=out_channel, kernel_size=3, stride=stride, bias=False, padding=1)

    self.downsample = downsample
    self.stride = stride

  def forward(self, x):
    identity = x
    if self.downsample is not None:
      identity = self.downsample(x)

    out = self.conv1(x)
    out = self.bn1(out)
    out = self.relu(out)

    out = self.conv2(out)
    out = self.bn2(out)

    out += identity
    out = self.relu(out)

    return out

class Resnet(nn.Module):
  def __init__(self, block, block_num):
    super(Resnet, self).__init__()

    self.in_channel = 64

    self.block1 = nn.Sequential(
        nn.Conv2d(in_channels=3, out_channels=self.in_channel, kernel_size=7, stride=2, padding=3, bias=False),
        nn.BatchNorm2d(self.in_channel),
        nn.LeakyReLU(inplace=True),
        nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
    )
    self.block2 = self._make_layer(block=block, channel=64, block_num=block_num[0], stride=1)
    self.block3 = self._make_layer(block=block, channel=128, block_num=block_num[1], stride=2)
    self.block4 = self._make_layer(block=block, channel=256, block_num=block_num[2], stride=2)
    self.block5 = self._make_layer(block=block, channel=512, block_num=block_num[3], stride=2)

    for m in self.modules():
      if isinstance(m, nn.Conv2d):
        nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')

  def _make_layer(self, block, channel, block_num, stride=1):
    downsample = None
    if stride != 1 or self.in_channel != channel*block.expansion:
      downsample = nn.Sequential(
          nn.Conv2d(in_channels=self.in_channel, out_channels=channel*block.expansion, kernel_size=1, stride=stride, bias=False),
          nn.BatchNorm2d(num_features=channel*block.expansion)
      )
    layers = []
    layers.append(block(in_channel=self.in_channel, out_channel=channel, downsample=downsample, stride=stride))
    self.in_channel = channel * block.expansion

    for _ in range(1, block_num):
      layers.append(block(in_channel=self.in_channel, out_channel=channel))

    return nn.Sequential(*layers)

  def forward(self, x):
    layer1 = self.block1(x)
    layer2 = self.block2(layer1)
    layer3 = self.block3(layer2)
    layer4 = self.block4(layer3)
    layer5 = self.block5(layer4)

    return layer2, layer3, layer4, layer5

def Resnet34():
  return Resnet(block=BasicBlock, block_num=[3, 4, 6, 3])
